module Main exposing (..)

import Html exposing (Html, a, button, code, div, h1, li, text, ul)
import Navigation
import UrlParser as Url exposing ((</>), (<?>), s, int, stringParam, top)

import Nav
import Logins


type alias Model =
  { logins : Logins.Model
  , route : Route
  }

type Route
  = Home
  | ShopIndex

type Msg
  = UrlChange Navigation.Location
  | ChildMsg Logins.Msg



main : Program Never Model Msg
main =
  Navigation.program UrlChange
    { init = init
    , view = view
    , update = update
    , subscriptions = subscriptions
    }



init : Navigation.Location -> ( Model, Cmd Msg )
init location =
  ( Model  Logins.initModel (parsedRoute location)
  , Cmd.none
  )

parsedRoute : Navigation.Location -> Route
parsedRoute location = 
 let
  maybeRoute = Url.parseHash myParser location
 in case maybeRoute of
  Nothing -> Home
  Just route -> route


myParser : Url.Parser (Route -> c) c
myParser =
 Url.oneOf
  [ Url.map Home top
  , Url.map ShopIndex (s "shop-end" </> s "index" ) ]


update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
  case msg of

    UrlChange location ->
      ( { model | route = parsedRoute location }
      , Cmd.none
      )

    ChildMsg msg ->
      let (newLogins,cmd) = (Logins.update msg model.logins)
      in 
       ( { model | logins = newLogins }
       , Cmd.map ChildMsg cmd
       )


subscriptions : Model -> Sub Msg
subscriptions model =
  Sub.none


view : Model -> Html Msg
view model =
 case model.route of
  Home -> homeView model
  ShopIndex -> shopIndexView
  

shopIndexView : Html msg
shopIndexView =
 text "还未完成"

homeView : Model -> Html Msg
homeView model = 
  div []
    [ Html.map never Nav.view
    , Html.map ChildMsg (Logins.view model.logins)
    ]